﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Biblioteca.Libros
{
    public partial class AltaLibros: Form
    {

        ClassDB GenClassBD;
        

       
        private static AltaLibros formInstancia = null;
        BindingSource miBS;

        public static AltaLibros Instancia()
        {
            if ((formInstancia == null) || (formInstancia.IsDisposed == true))
            {
                formInstancia = new AltaLibros();
            }
            formInstancia.BringToFront();
            return formInstancia;
        }

        public AltaLibros()
        {
            InitializeComponent();
            miBS = new BindingSource();
            GenClassBD = new ClassDB();
            
            comboBox3.Items.Clear();
            

            ClassDB consulta = new ClassDB();

            miBS = consulta.Consulta("Select nombre from Autores order by nombre ");
            if (miBS.Count >= 0)
            {
                
                comboBox3.DataSource = miBS;
                comboBox3.DisplayMember = "nombre";
                comboBox3.SelectedIndex = -1;
                

            }
            else
                MessageBox.Show("No hay autores cargados","Busqueda de Autores", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);


           
          }

        

        private void button4_Click(object sender, EventArgs e)
        {
            if ((textBoxTit.Text.Length > 0) & (textBoxEdicion.Text.Length > 0) & (textBoxEditorial.Text.Length > 0) & (comboBox3.Text.Length > 0) & (comboBox4.Text.Length > 0))
            {
                DialogResult op = MessageBox.Show("¿Los datos del libro son correctos?", "Confirmacion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (op == DialogResult.Yes)
                {


                    GenClassBD.InsertarRegistro("Libros", "titulo, autor, editorial, edicion, estado, disponibilidad, tematica", "'" +
                        textBoxTit.Text + "','" + comboBox3.Text + "','" + textBoxEditorial.Text + "','" + textBoxEdicion.Text + "','" + "activo" +
                         "','" + "true" + "','" + comboBox4.Text + "'");
                    MessageBox.Show("El libro fue ingresado correctamente", "Ingreso de libro", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    textBoxEdicion.Text = "";
                    textBoxTit.Text = "";
                    textBoxEditorial.Text = "";
                    comboBox3.Text = "";
                    comboBox4.Text = "";
                }
                else { }

                

            }
            else { MessageBox.Show("Debe completar todos los datos...", "Ingreso de libro", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }


        }

        private void AltaLibros_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {

            groupBox32.Enabled = false;
            groupBoxNuevoautor.Visible = true;
            textBox1Autor.Focus();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBoxTit.Text = "";
            textBoxEditorial.Text = "";
            textBoxEdicion.Text = "";
            comboBox3.Text = "";

            
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBoxEditorial_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBoxEdicion_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void comboBox3_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox1Autor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {

            if (textBox1Autor.Text.Length > 0)
            {
                GenClassBD = new ClassDB();
               Object autor = GenClassBD.ConsultaSimple("Select nombre from Autores where nombre = '" + textBox1Autor.Text +"'");
               if ((Convert.ToString(autor)).Length == 0)
               {
                   GenClassBD.InsertarRegistro("Autores", "nombre", "'" + textBox1Autor.Text + "'");
                   MessageBox.Show("El autor se ingreso correctamente", "Ingreso de autor", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                   textBox1Autor.Text = "";
                   groupBox32.Enabled = true;
                   groupBoxNuevoautor.Visible = false;

                   ClassDB consulta = new ClassDB();

                   miBS = consulta.Consulta("Select nombre from Autores order by nombre ");
                   if (miBS.Count >= 0)
                   {

                       comboBox3.DataSource = miBS;
                       comboBox3.DisplayMember = "nombre";



                   }
                   else
                       MessageBox.Show("No hay autores cargados", "Busqueda de Autores", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        

               }
               else
               {
                   MessageBox.Show("El autor ya existe en la base de datos", "Ingreso de autor", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); textBox1Autor.Text = "";
                   groupBox32.Enabled = true;
                   groupBoxNuevoautor.Visible = false;
               }

            }
            else { MessageBox.Show("Debe ingresar un nombre de autor", "Ingreso de autor", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }

        }

        private void textBoxEdicion_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonCanc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
        }

 
    
}
